﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace TankBattle.Models
{
    public class MenuBar
    {
        #region variable 
        protected Vector2 currentPosition;
        protected Sprite sprite;
        protected bool isMove;

        protected int flag;
        protected int gameState;
        #endregion 

        #region Construction 
        public MenuBar()
        {
            Initiallize();
        }
        public MenuBar(Texture2D texture)
        {
            sprite = new Sprite(texture,
                   new Vector2(Constant.MENU_BAR_SIZE_X, Constant.MENU_BAR_SIZE_Y),
                   new Vector2(Constant.MENU_BAR_EFF_SHEET_X, Constant.MENU_BAR_EFF_SHEET_Y),
                   Constant.RATE_MENU_BAR,
                   false);
            Initiallize();
        }
        #endregion 

        #region Main Function 
        public void Update()
        {
            sprite.Update();
            SetKeyDown();
            if (!SetMove() && !isMove) SetState();
        }
        public void Draw(SpriteBatch spriteBatch)
        {
            if (Stop())
                sprite.Draw(spriteBatch, currentPosition);
        }
        #endregion 

        #region Properties
        public Vector2 Position
        {
            get { return currentPosition; }
        }
        public int GameState
        {
            get { return gameState; }
        }
        protected void Initiallize()
        {
            flag = 0;
            currentPosition = new Vector2(
                Constant.MENU_BAR_X,
                Constant.MENU_BAR_Y);
        }
        #endregion 

        #region Other Function
        protected void SetKeyDown()
        {
            if (Keyboard.GetState().IsKeyDown(Keys.Down))
            {
                flag = Constant.MOVE_DOWN;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Up))
            {
                flag = Constant.MOVE_UP;
            }
        }
        protected bool SetMove()
        {
            switch (flag)
            {
                case (Constant.MOVE_DOWN):
                    if (currentPosition.Y < Constant.MENU_BAR_END)
                    currentPosition.Y += Constant.MENU_BAR_STEP;               
                    break;
                case (Constant.MOVE_UP):
                    if (currentPosition.Y > Constant.MENU_BAR_START)
                    currentPosition.Y -= Constant.MENU_BAR_STEP;
                    break;
            }
            if (Stop())
            {
                flag = 0;
                return false;
            }
            else
            {
                isMove = false;
                return true;
            }
        }
        protected void SetState()
        {
            isMove = true;
            int current = (int)currentPosition.Y;
            switch (current)
            {
                case (Constant.MENU_BAR_START + Constant.MENU_STATE_HELP * Constant.MENU_CELL):
                    gameState = Constant.STATUS_HELP;
                    sprite.SetCurrentFrame(new Vector2(0, Constant.MENU_STATE_HELP));
                    break;
                case (Constant.MENU_BAR_START + Constant.MENU_STATE_NEW * Constant.MENU_CELL):
                    gameState = Constant.STATUS_NEW;
                    sprite.SetCurrentFrame(new Vector2(0, Constant.MENU_STATE_NEW));
                    break;
                case (Constant.MENU_BAR_START + Constant.MENU_STATE_SCORE * Constant.MENU_CELL):
                    gameState = Constant.STATUS_HIGH_SCORE;
                    sprite.SetCurrentFrame(new Vector2(0, Constant.MENU_STATE_SCORE));
                    break;
                case (Constant.MENU_BAR_START + Constant.MENU_STATE_EXIT * Constant.MENU_CELL):
                    gameState = Constant.STATUS_EXIT;
                    sprite.SetCurrentFrame(new Vector2(0, Constant.MENU_STATE_EXIT));
                    break;
                default:
                    gameState = Constant.STATUS_MENU;
                    break;
            }
        }
        protected bool Stop()
        {
            return(currentPosition.Y - Constant.MENU_BAR_START)
                % Constant.MENU_CELL == 0;
        }
        #endregion 
    }
}
